/**
 * 83. Remove Duplicates from Sorted List
 * https://leetcode.com/problems/remove-duplicates-from-sorted-list/
 */
package lc83;

public class Main {
  public static void main(String[] args) {
    ListNode head = new ListNode(1);
    ListNode c = head;
    for (int i = 1; i < 10; i++) {
      c.next = new ListNode(i);
      c = c.next;
    }
    c.next = new ListNode(9);
    ListNode result = new Solution().deleteDuplicates(head);
    while (result != null) {
      System.out.print(result.val + " ");
      result = result.next;
    }
  }
}

class Solution {
  public ListNode deleteDuplicates(ListNode head) {
    if (head == null) return null;
    ListNode result = new ListNode(head.val);
    ListNode cursor = result;
    while (head != null) {
      if (head.val != cursor.val) {
        cursor.next = new ListNode(head.val);
        cursor = cursor.next;
      }
      head = head.next;
    }
    return result;
  }
}

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}
